package com.dating.base.module;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.dating.base.bean.Permission;
import com.dating.base.dto.Pager;
import com.dating.base.service.PermissionService;
import com.dating.base.service.RolePermissionService;
import com.dating.base.service.UserPermissionService;
import com.dating.core.entries.Result;
import com.dating.core.ext.BaseModule;



public class PermissionModule extends BaseModule{

	@Inject
	private PermissionService permissionService;
	
	@Inject
	private UserPermissionService userPerService;
	
	@Inject
	private RolePermissionService rolePerService;
	
	
	public String _getNameSpace() {
		return "permission";
	}
	
	/**
	 * 权限列表
	 * @return
	 */
	@At("permission/list")
	@Ok("beetl:templates/pages/base/permission_list.html")
	public Result toList(){
		Pager pager = createPager("key");
		return Result.success().addData("queryResult", permissionService.findAllByPage(pager));
	}
	
	/**
	 * to 编辑页
	 * @param id
	 * @return
	 */
	@At("permission/toEdit")
	@Ok("beetl:templates/pages/base/permission_edit.html")
	public Result toEdit(int id){
		Permission perm = permissionService.findById(id);
		return Result.success().addData("permission", perm);
	}
	
	
	/**
	 * 增加权限
	 * @param permission
	 * @return
	 */
	@At("permission/add")
	@Ok("json")
	public Result add(@Param("..")Permission permission){
		Permission perm = permissionService.findByName(permission.getName());
		if(perm == null){
			permissionService.save(permission);
		}else{
			return Result.fail(String.format("权限名%s已存在", permission.getName()));
		}
		return Result.success();
	}
	
	
	/**
	 * 修改权限
	 * @param permission
	 * @return
	 */
	@At("permission/update")
	@Ok("json")
	public Result update(@Param("..")Permission permission){
		Permission perm = permissionService.findByName(permission.getName());
		if(perm == null || perm.getId().equals(permission.getId())){
			permissionService.updateIgnoreNull(permission);
		}else{
			return Result.fail(String.format("权限名%s已存在", permission.getName()));
		}
		return Result.success();
	}
	
	
	/**
	 * 删除指定id的权限信息
	 * @param ids
	 * @return
	 */
	@At("permission/batchDel")
	@Ok("json")
	public Result batchDel(String ids){
		int count = permissionService.batchDel(ids);
		if(count > 0){
			return Result.success();
		}else{
			return Result.fail("删除权限失败！");
		}
		
	}
	
	
	@At("permission/getTree")
	@Ok("json")
	public Object getTree(String ids){
		List<Permission> perms = permissionService.findAll();
		return toTree(perms);
	}
	
	
	
	/**
	 * 生成权限树结构
	 * @param list
	 * @return
	 */
	private List<Map<String, Object>> toTree(List<Permission> list){
		long ctime = System.currentTimeMillis();
		List<Map<String, Object>> trees = new ArrayList<Map<String,Object>>();
		Map<String, Object> data = null;
		for (Permission per : list) {
			if(per.getLevel() == 0){
				data = new HashMap<String, Object>();
				data = new HashMap<String, Object>();
				data.put("id", per.getId());
				data.put("name", per.getDescription());
				data.put("value", per.getName());
				//data.put("level", per.getLevel());
				//data.put("icon", per.getIcon());
				List<?> childrens = findChildren(list, per.getName());
				if(childrens.size() > 0){
					data.put("children", childrens);
				}
				trees.add(data);
			}
		}
		log.debug("生成树结构耗时：("+ (System.currentTimeMillis() - ctime) +")ms ");
		return trees;
	}
	
	/**
	 * 树结构查找子节点
	 * @param list
	 * @param pname
	 * @return
	 */
	private List<Map<String, Object>> findChildren(List<Permission> list, String pname){
		List<Map<String, Object>> childrens = new ArrayList<Map<String,Object>>();
		Map<String, Object> dataMap = null;
		for (Permission per : list) {
			if(pname.equals(per.getNeedPermission())){
				dataMap = new HashMap<String, Object>();
				dataMap.put("id", per.getId());
				dataMap.put("name", per.getDescription());
				dataMap.put("value", per.getName());
				//dataMap.put("level", per.getLevel());
				//dataMap.put("icon", per.getIcon());
				List<?> childs = findChildren(list, per.getName());
				if(childs.size() > 0){
					dataMap.put("children", childs);
				}
				childrens.add(dataMap);
			}
		}
		return childrens;
	}
	
}
